package com.aaa.service.impl;


import com.aaa.dao.UserDao;
import com.aaa.entity.AlertCode;
import com.aaa.entity.Dept;
import com.aaa.entity.Userinfo;
import com.aaa.service.UserinfoService;
import com.aaa.util.MyUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ：zhouhangyong
 * @date ：Created in 2022/6/22 11:59
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class UserinfoServiceImpl implements UserinfoService {
    @Autowired
    private UserDao userDao;
    private AlertCode alertCode;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public Page userinfoList(Map<String, Object> map) {
        Page listWithPage = new Page();
        Object limit1 = map.get("limit");
        if (limit1 != null && limit1 != "") {
            Integer limit = Integer.parseInt(limit1 + "");
            Integer page = Integer.parseInt(map.get("page") + "");
//            page = (page - 1) * limit;
//            map.put("page", page);
            //开始分页
            Page<Userinfo> pageLimit = PageHelper.startPage(page, limit);
            List<Userinfo> userList = userDao.userList(map);
            listWithPage= (Page) userList;

        }
//        List<Userinfo> userList = userDao.userList(map);
//        Integer userPage = userDao.userPage(map);
//        AlertCode alertCode = new AlertCode(0, "", (int) userPage, userList);
        return listWithPage;



//        return alertCode;
    }

    @Override
    public AlertCode deptList() {
        List<Dept> deptList = userDao.deptList();
        return new AlertCode(0, "", deptList);
    }

    @Override
    public AlertCode addUserinfo(Userinfo userinfo) {
        Map<String, String> makePasswordSalt = MyUtil.makePasswordSalt(userinfo.getPassword());
        userinfo.setSalt(makePasswordSalt.get("salt"));
        userinfo.setPassword(makePasswordSalt.get("password"));
        Integer addAll = userDao.addAll(userinfo);
        System.out.println(addAll);
        if (addAll != null && addAll > 0) {
            return new AlertCode(0, "添加成功");
        } else {
            return new AlertCode(1, "添加失败");
        }

    }

    @Override
    public AlertCode updateUserinfo(Userinfo userinfo) {
        Integer updateUserinfo = userDao.updateUserinfo(userinfo);
        if (updateUserinfo > 0) {
            return new AlertCode(0, "修改成功");
        } else {
            return new AlertCode(1, "更新失败");
        }
    }

    @Override
    public AlertCode deleteUserinfo(String userid) {
        Integer userinfo = userDao.deleteUserinfo(userid);
        if (userinfo != null && userinfo > 0) {
            return new AlertCode(0, "删除成功");
        } else {
            return new AlertCode(1, "删除失败");
        }
    }

    @Override
    public AlertCode updatePassword(Userinfo userinfo) {
        Map<String, String> makePasswordSalt = MyUtil.makePasswordSalt(userinfo.getPassword());
        userinfo.setSalt(makePasswordSalt.get("salt"));
        userinfo.setPassword(makePasswordSalt.get("password"));
        Integer updatePassword = userDao.updatePassword(userinfo);
        System.out.println(updatePassword);
        if (updatePassword != null && updatePassword > 0) {
            return new AlertCode(0, "添加成功");
        } else {
            return new AlertCode(1, "添加失败");
        }
    }

    @Override
    public AlertCode updateServiceKey(Integer userid) {
        List<Userinfo> userinfoList = userDao.updateKey(userid);
        return new AlertCode(0,"",userinfoList);
    }
}
